﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
    /// <summary>
    /// 角色表
    /// </summary>
    public class RolesDal : BaseDal<SysRoles>
    {
        public override string tablename
        {
            get { return "SysRoles"; }
        }

        public override Dictionary<string, bool> PKName
        {
            get
            {
                Dictionary<string, bool> dic = new Dictionary<string, bool>();
                dic.Add("roles_Id", true);
                return dic;
            }
        }

        string str = ConfigurationManager.ConnectionStrings["db"].ConnectionString;

        /// <summary>
        /// 反填角色信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<SysRoles> BackRoles(int id)
        {
            using (SqlConnection con = new SqlConnection(str))
            {
                con.Open();
                string sql = "select * from SysRoles where roles_Id=" + id;
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataAdapter dat = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                dat.Fill(dt);
                List<SysRoles> role = new List<SysRoles>();
                foreach (DataRow i in dt.Rows)
                {
                    SysRoles r = new SysRoles();
                    r.roles_Id = Convert.ToInt32(i["Roles_Id"]);
                    r.roles_Name = i["Roles_Name"].ToString();
                    r.roles_Remarks = i["Roles_Remarks"].ToString();
                    r.roles_IsState = Convert.ToInt32(i["Roles_IsState"]);
                    r.roles_CreatePeople = i["Roles_CreatePeople"].ToString();
                    role.Add(r);
                }
                return role;
            }
        }

        /// <summary>
        /// 停用角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool StopRole(int id)
        {
            using (SqlConnection con = new SqlConnection(str))
            {
                con.Open();
                string sql = "update SysRoles set Roles_IsState=1 where Roles_Id=" + id;
                SqlCommand cmd = new SqlCommand(sql, con);
                return cmd.ExecuteNonQuery() > 0;
            }
        }

        /// <summary>
        /// 启用角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool StartRole(int id)
        {
            using (SqlConnection con = new SqlConnection(str))
            {
                con.Open();
                string sql = "update SysRoles set Roles_IsState=0 where Roles_Id=" + id;
                SqlCommand cmd = new SqlCommand(sql, con);
                return cmd.ExecuteNonQuery() > 0;
            }
        }
    }
}
